<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="open('add')"
          >
            添加面经
          </el-button>
        </div>
      </template>
      <template>
        <el-table :data="list" style="width: 100%">
          <el-table-column prop="stem" label="标题"> </el-table-column>
          <el-table-column prop="creator" label="作者"> </el-table-column>
          <el-table-column prop="likeCount" label="点赞"> </el-table-column>
          <el-table-column prop="views" label="浏览数"> </el-table-column>
          <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
          <el-table-column label="操作" width="120px">
            <!-- <template slot-scope="scope"> -->
            <template #default="scope">
              <div class="actions">
                <i
                  class="el-icon-view"
                  @click="open('preview', scope.row.id)"
                ></i>
                <i
                  class="el-icon-edit-outline"
                  @click="open('edit', scope.row.id)"
                ></i>
                <i class="el-icon-delete" @click="del(scope)"></i>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="pageSize"
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- before-close关闭抽屉前的回调函数,一旦设置抽屉就不会直接关闭,需要在回调函数里边关闭 -->
    <el-drawer
      :title="title[type]"
      :visible.sync="isShow"
      direction="rtl"
      :before-close="handleClose"
      size="60%"
    >
      <span>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="标题">
            <el-input v-model="form.title"  placeholder='面经标题'></el-input>
          </el-form-item>
          <el-form-item label="内容">
            <el-input v-model="form.text" placeholder='模拟富文本编辑器'></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" >确认</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </span>
    </el-drawer>
  </div>
</template>

<script>
import { getArticleList } from '@/api/article'
export default {
  name: 'article-page',
  data () {
    return {
      form: {
        title: '',
        text: ''
      },
      current: 1,
      pageSize: 10,
      list: [],
      total: 0,
      isShow: false,
      type: 'add', // 状态
      title: {
        add: '添加面经',
        preview: '面经预览',
        edit: '修改面经'
      }
    }
  },
  created () {
    this.initDate()
  },
  methods: {
    async initDate () {
      const res = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      // console.log(res)
      // console.log(res.data.rows)
      // console.log(res.data.total)
      this.list = res.data.rows
      this.total = res.data.total
    },
    del (scope) {
      console.log(scope.row.id)
    },
    handleCurrentChange (val) {
      console.log(val)
      this.current = val
      this.initDate()
    },
    open (type, id) {
      console.log(type, id)
      this.type = type
      // 显示抽屉
      this.isShow = true
    },
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    }
  }
  // computed: {
  //   title () {
  //     let title = ''
  //     if (this.type === 'add') title = '添加面经'
  //     if (this.type === 'preview') title = '面经预览'
  //     if (this.type === 'edit') title = '编辑面经'
  //     return title
  //   }
  // }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
